<?php

namespace app\admin\model;

use app\common\model\MoneyLog;
use app\common\model\ScoreLog;
use think\Db;
use think\Model;

class User extends Model
{

    // 表名
    protected $name = 'user';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'prevtime_text',
        'logintime_text',
        'jointime_text'
    ];

    public function getOriginData()
    {
        return $this->origin;
    }

    /*
     * 获取各等级人数
     */
//    public static function levelTotalList(){
//        $level = [1=>0,2=>0,3=>0,4=>0,5=>0,6=>0];
//
//        foreach ($level as $lev => &$total ) {
//            $total = intval(Db::name('user') ->where(['user_level'=> $lev])->count());
//        }
//        return $level;
//    }
    /*
     * 获取各叠加等级人数
     */
    public static function levelSuperpositionTotalList(){
        $level = [1=>0,2=>0,3=>0,4=>0,5=>0,6=>0];
        foreach ($level as $lev => &$total ) {
            $total = intval(Db::name('user') ->where(['user_level'=> ['>=',$lev]])->count());
        }
        return $level;

    }

    /*
     * 获取各等级分红比例
     */

    public static function DiPercent(){
        $level = [1=>30,2=>30,3=>20,4=>10,5=>5,6=>5];
        return $level;
    }

    /*
     * 计算各等级分成金额
     */
    public static function DiLevelMoney($totalMoney)
    {
        $percentMoney = User::DiPercent();
        //百分变成各等级分红总金额
        foreach ($percentMoney as $k=>$item) {
            $percentMoney[$k] = ($item * $totalMoney);
        }
//        echo '<pre>';
//        var_dump($percentMoney);
        //每个等级分多少钱
        $userTotalByLevel =  User::levelSuperpositionTotalList();
        foreach ($userTotalByLevel as $level => $item) {
            if(!empty($percentMoney[$level]) && !empty($item)){
                $temp =[
                    'leveMoney'=>$percentMoney[$level], //总金额
                    'money'=>intval($percentMoney[$level]/$item), //每人能分多少钱（含高等级用户）
                    'sum'=>$item,
                ];
                $item = $temp;

            }else{
                $temp =[
                    'leveMoney'=>$percentMoney[$level], //总金额
                    'money'=>0,
                    'sum'=>0,
                ];
                $item = $temp;
            }
            $userTotalByLevel[$level] = $item;
        }


        //高等级累计低等级分红金额
        foreach ($userTotalByLevel as $level=>$item) {
            $totalMoney = 0;
            switch ($level){
                case $level>=6: $totalMoney+=$userTotalByLevel[6]['money'];
                case $level>=5: $totalMoney+=$userTotalByLevel[5]['money'];
                case $level>=4: $totalMoney+=$userTotalByLevel[4]['money'];
                case $level>=3: $totalMoney+=$userTotalByLevel[3]['money'];
                case $level>=2: $totalMoney+=$userTotalByLevel[2]['money'];
                case $level>=1: $totalMoney+=$userTotalByLevel[1]['money'];
            }
            //转为元单位/保留两位小数
//            $item['leveMoney'] = floor($item['leveMoney'])/100;
//            $item['money'] = floor($item['money'])/100;
            $userTotalByLevel[$level]['totalMoney'] = $totalMoney;
        }
        foreach ($userTotalByLevel as &$item) {
            $item['leveMoney'] = floor($item['leveMoney'])/100;
            $item['money'] = floor($item['money'])/100;
            $item['totalMoney'] = floor($item['totalMoney'])/100;
        }
        return $userTotalByLevel;
    }


    protected static function init()
    {

    }

    public function getGenderList()
    {
        return ['1' => __('Male'), '0' => __('Female')];
    }

    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }


    public function getPrevtimeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['prevtime'] ?? "");
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getLogintimeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['logintime'] ?? "");
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function getJointimeTextAttr($value, $data)
    {
        $value = $value ? $value : ($data['jointime'] ?? "");
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPrevtimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setLogintimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setJointimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setBirthdayAttr($value)
    {
        return $value ? $value : null;
    }

    public function group()
    {
        return $this->belongsTo('UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function invite()
    {
        return $this->belongsTo('User', 'parent_user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
